<template>
  <el-dialog
    :title="!dataForm.userId ? '添加用户' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="用户头像" prop="userHead">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:88/api/authserver/user/upload/"
          :show-file-list="false"
          :on-success="handleAvatarSuccess">
          <img v-if="dataForm.userHead" :src="dataForm.userHead" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    <el-form-item label="用户名称" prop="userName" >
      <el-input v-model="dataForm.userName" placeholder="用户名称"></el-input>
    </el-form-item>
    <el-form-item label="用户密码" prop="userPassword">
      <el-input v-model="dataForm.userPassword" placeholder="用户密码" show-password></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="userNickname">
      <el-input v-model="dataForm.userNickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="userGender">
      <el-radio v-model="dataForm.userGender" label="男">男</el-radio>
      <el-radio v-model="dataForm.userGender" label="女">女</el-radio>
    </el-form-item>
    <el-form-item label="所在地" prop="userAddressId">
      <div class="block">
        <el-cascader
          v-model="value"
          :options="options"
          :props="props"
          @change="handleChange"
          clearable></el-cascader>
      </div>
    </el-form-item>
    <el-form-item label="真实姓名" prop="userRealName">
      <el-input v-model="dataForm.userRealName" placeholder="真实姓名"></el-input>
    </el-form-item>
    <el-form-item label="生日" prop="userBirthday">
      <div class="block">
        <el-date-picker
          v-model="dataForm.userBirthday"
          type="datetime"
          placeholder="选择日期时间"
          value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </div>
    </el-form-item>
    <el-form-item label="身份证号" prop="userIdCard">
      <el-input v-model="dataForm.userIdCard" placeholder="身份证号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<script>
  export default {
    data () {
      return {
        value:[],
        options:[],
        addressList:[],
        props:{
          value:"addressId",
          label:"addressName",
          children:"children"
        },
        visible: false,
        treeName:'',
        address:[{
          value:"addressId",
          label:"addressName",
          children:"children"
        }],
        dataForm: {
          userId: 0,
          userName: '',
          userPassword: '',
          userHead: '',
          userNickname: '',
          userGender: '',
          userAddressId: '',
          userRealName: '',
          userBirthday: '',
          userIdCard: '',
          nickname: '',
          socialUid: '',
          accessToken: '',
          expiresIn: '',
          pageIndex: 1,
          pageSize: 5,
          totalPage: 0,
          key:''
        },
        dataRule: {
          userName: [
            { required: true, message: '用户名称不能为空', trigger: 'blur' }
          ],
          userPassword: [
            { required: true, message: '用户密码不能为空', trigger: 'blur' }
          ],
          userHead: [
            { required: true, message: '用户头像不能为空', trigger: 'blur' }
          ],
          userNickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          userGender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          userAddressId: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ],
          userRealName: [
            { required: true, message: '真实姓名不能为空', trigger: 'blur' }
          ],
          userBirthday: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ],
          userIdCard: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '微博昵称不能为空', trigger: 'blur' }
          ],
          socialUid: [
            { required: true, message: '微博uid不能为空', trigger: 'blur' }
          ],
          accessToken: [
            { required: true, message: '微博令牌不能为空', trigger: 'blur' }
          ],
          expiresIn: [
            { required: true, message: '微博登录过期时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getAddressList();
    },
    methods: {
      getAddressList () {
        this.$http({
          url: this.$http.adornUrl('/authserver/address/treeList'),
          method: 'get',
          params: null
        }).then(({data}) => {
          console.log(data)
            this.options = data
        })
      },
      getAddress (addressId) {
        this.$http({
          url: this.$http.adornUrl('/authserver/address/treeById'),
          method: 'get',
          params: this.$http.adornParams({
            'addressId': addressId
          })
        }).then(({data}) => {
          console.log(data)
          data.forEach(item =>{
            this.options.label = item.addressName
            this.options.value = item.addressId
          })
        })
      },
      handleChange(value) {
        this.dataForm.userAddressId = value
      },
      handleAvatarSuccess(res, file) {
        this.dataForm.userHead = res
      },
      init (id) {
        this.value = []
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/authserver/user/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.user.userName
                this.dataForm.userPassword = data.user.userPassword
                this.dataForm.userHead = data.user.userHead
                this.dataForm.userNickname = data.user.userNickname
                this.dataForm.userGender = data.user.userGender
                this.dataForm.userAddressId = data.user.userAddressId
                this.dataForm.userRealName = data.user.userRealName
                this.dataForm.userBirthday = data.user.userBirthday
                this.dataForm.userIdCard = data.user.userIdCard
                this.dataForm.nickname = data.user.nickname
                this.dataForm.socialUid = data.user.socialUid
                this.dataForm.accessToken = data.user.accessToken
                this.dataForm.expiresIn = data.user.expiresIn
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/authserver/user/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'userName': this.dataForm.userName,
                'userPassword': this.dataForm.userPassword,
                'userHead': this.dataForm.userHead,
                'userNickname': this.dataForm.userNickname,
                'userGender': this.dataForm.userGender,
                'userAddressId': this.dataForm.userAddressId.toString(),
                'userRealName': this.dataForm.userRealName,
                'userBirthday': this.dataForm.userBirthday,
                'userIdCard': this.dataForm.userIdCard,
                'nickname': this.dataForm.nickname,
                'socialUid': this.dataForm.socialUid,
                'accessToken': this.dataForm.accessToken,
                'expiresIn': this.dataForm.expiresIn
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
    }
  }
</script>
